package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.aop.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        // 把套餐数据封装到套餐对象中
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 设置套餐新增完毕即启售
//        setmeal.setStatus(StatusConstant.ENABLE);

        // 插入套餐数据
        log.info("[新增套餐业务层]{}", setmeal);
        setmealMapper.insertSetmeal(setmeal);

        // 获取套餐中包含的菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 进行判断 并插入菜品套餐关系表
        if (setmealDishes == null && setmealDishes.size() <= 0) {
            throw new SetmealEnableFailedException("当前套餐内容还没有菜品 无法添加");
        }

        for (SetmealDish setmealDish : setmealDishes) {
            // 将当前套餐id封装入菜品信息中
            setmealDish.setSetmealId(setmeal.getId());
            // 将菜品信息插入菜品套餐关系表中
            log.info("[新增套餐业务层菜品]{}", setmealDish);
            setmealDishMapper.insertSetmealInfo(setmealDish);
        }
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageListSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
//        // 设置分页格式
//        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
//
//        // 查询数据
//        List<Setmeal> setmeals = setmealMapper.selectSetmeal(setmealPageQueryDTO);
//        log.info("[业务层套餐分页查询]{}", setmeals);
//        if (setmeals == null) {
//            throw new SetmealEnableFailedException(MessageConstant.NOT_FIND_SETMEAL);
//        }
//        Page<Setmeal> page = (Page<Setmeal>) setmeals;
//
//        List<SetmealVO> pageResult = page.getResult().stream().map((setmeal -> {
//            SetmealVO setmealVO = new SetmealVO();
//            BeanUtils.copyProperties(setmeal, setmealVO);
//            Long categoryId = setmealVO.getCategoryId();
//
//            // 根据分类id 查询出套餐所属的分类名称
//            Category category = categoryService.selectNameByCategoryId(categoryId);
//            if (category != null) {
//                setmealVO.setCategoryName(category.getName());
//            }
//            return setmealVO;
//        })).collect(Collectors.toList());

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = (Page<SetmealVO>) setmealMapper.selectSetmeal(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {
        // 查询
        Setmeal setmeal = setmealMapper.selectSetmealById(id);

        // 创建SetmealVO对象 封装数据
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        // 查询当前套餐包含的菜品
        List<SetmealDish> dishes = setmealDishMapper.selectBySetmealId(id);
        log.info("[修改菜品业务层套餐菜品]{}", dishes);
        Category category = categoryService.selectNameByCategoryId(setmeal.getCategoryId());

        setmealVO.setSetmealDishes(dishes);
        setmealVO.setCategoryName(category.getName());
        log.info("[修改菜品业务层]{}", setmealVO);
        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 修改套餐基本信息
        setmealMapper.updateSetmeal(setmeal);

        // 删除原有的套餐对应的菜品信息
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        // 将新的菜品信息插入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insertSetmealInfo(setmealDish);
        }
    }

    /**
     * 修改套餐状态
     *
     * @param status
     * @param id
     */
    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        if (status == StatusConstant.ENABLE) {
            // 当前操作是启售套餐操作 应该判断其中包含的菜品内容

            // 获取当前套餐包含的所有菜品信息
            List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
            // 收集当前套餐包含的所有菜品的id
            List<Long> ids = setmealDishes.stream().map(SetmealDish::getDishId).collect(Collectors.toList());

            // 遍历菜品id
            for (Long dishId : ids) {
                Dish dish = dishMapper.selectDishById(dishId);
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    // 当前菜品是禁用状态 不能启售当前套餐
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        // 禁用套餐操作 无需判断
        // 修改套餐状态
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.updateSetmeal(setmeal);
    }

    /**
     * 删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectSetmealById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                // 当前套餐在启售中 无法删除
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        // 可以进行删除
        // 删除套餐信息
        setmealMapper.deleteSetmealByIds(ids);

        for (Long id : ids) {
            // 删除套餐对应的菜品信息
            setmealDishMapper.deleteBySetmealId(id);
        }
    }

    /**
     * 根据分类id查询套餐以及套餐包含的菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> selectByCategoryId(Long categoryId) {
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);

        return setmealMapper.selectSetmealByCategoryId(setmeal);
    }
}
